import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        String s = "dddddddddddd";
        String[] words = new String[]{"dddd","dddd"};
        Solution solution = new Solution();
        solution.findSubstring(s,words);
    }
}
class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        Map<String,Integer> hash1 = new HashMap<>();
        List<Integer> ret = new ArrayList<>();
        for (String i:words) hash1.put(i,hash1.getOrDefault(i,0)+1);
        int m = words[0].length(),n = words.length;
        for (int i = 0;i < m;i++) {
            //进窗口
            Map<String,Integer> hash2 = new HashMap<>();
            for (int left = i,right = i,count = 0;right+m<=s.length();right+=m) {
                String in = s.substring(right,right+m);
                hash2.put(in,hash2.getOrDefault(in,0)+1);
                if (hash1.getOrDefault(in,0)>=hash2.get(in)) {
                    count++;//有效字符串维护count
                }
                //出窗口
                if (right-left+1>m*n) {
                    String out = s.substring(left,left+m);
                    if (hash1.getOrDefault(out,0)>=hash2.get(out)) count--;//维护count
                    hash2.put(out,hash2.get(out)-1);
                    left+=m;
                }
                //更新结果
                if (count==n) ret.add(left);
            }
        }
        return ret;
    }
}